package com.bw.mallshop.widegt;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;
import com.bw.mallshop.R;

/**
 * @author KK
 * @description:
 * @date :2021/9/9 16:06
 * 自定义底部导航栏
 */
public class BottomButton extends RelativeLayout implements View.OnClickListener {

    private LinearLayout linearLayout;//底部文字和图片的容器
    private ImageView img;//图片
    private TextView contentTv;//图片下的文字
    private BottomSelectListener listener;
    private boolean selFlag = false;
    private int namalSize = ConvertUtils.sp2px(5);
    private int selSize = ConvertUtils.sp2px(7);
    private int nomalColor = Color.BLACK;//默认字体颜色
    private int selColor = Color.RED;//选中字体颜色
    private int nomalImg = 0;//未选中图片
    private int selImg = 0;//选中图片
    private String content = "";//底部文字内容

    public BottomButton setListener(BottomSelectListener listener) {
        this.listener = listener;
        return this;

    }

    //设置显示的文字内容
    public BottomButton setContent(String content) {
        this.content = content;
        return this;
    }

    //设置是否选中
    public BottomButton setSelFlag(boolean selFlag) {
        this.selFlag = selFlag;
        refresh();
        return this;
    }

    //未选中字体大小
    public BottomButton setNamalSize(int namalSize) {
        this.namalSize = ConvertUtils.sp2px(namalSize);
        return this;
    }

    //选中字体大小
    public BottomButton setSelSize(int selSize) {
        this.selSize = ConvertUtils.sp2px(selSize);
        return this;
    }

    //未选中字体颜色
    public BottomButton setNomalColor(int nomalColor) {
        this.nomalColor = nomalColor;
        return this;
    }

    //选中字体颜色
    public BottomButton setSelColor(int selColor) {
        this.selColor = selColor;
        return this;
    }

    //未选中图片
    public BottomButton setNomalImg(int nomalImg) {
        this.nomalImg = nomalImg;
        return this;
    }

    //选中图片
    public BottomButton setSelImg(int selImg) {
        this.selImg = selImg;
        return this;
    }

    //构造器
    public BottomButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        //自定义属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.bottom_arrs);

        selFlag = typedArray.getBoolean(R.styleable.bottom_arrs_selflag, false);
        typedArray.recycle();//释放
        init();
    }

    //初始化方法
    private void init() {
        linearLayout = new LinearLayout(getContext());
        LayoutParams linnerLp = new LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        linnerLp.addRule(CENTER_IN_PARENT);//布局居中
        linearLayout.setLayoutParams(linnerLp);
        //设置排列方向
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        img = new ImageView(getContext());
        LinearLayout.LayoutParams imgLp = new LinearLayout.LayoutParams(
                ConvertUtils.dp2px(25),
                ConvertUtils.dp2px(25));
        img.setLayoutParams(imgLp);
        linearLayout.addView(img);
        contentTv = new TextView(getContext());
        LinearLayout.LayoutParams contenLp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        contentTv.setLayoutParams(contenLp);
        linearLayout.addView(contentTv);
        addView(linearLayout);

        setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        selFlag = true;
        if (listener != null) {
            listener.onSelect(getId());
        }
    }

    //刷新UI
    public void refresh() {
        int image = nomalImg;
        int tvcolor = nomalColor;
        int tvsize = namalSize;
        if (selFlag) {
            image = selImg;
            tvcolor = selColor;
            tvsize = selSize;
        }
        img.setImageResource(image);
        contentTv.setTextColor(tvcolor);
        contentTv.setTextSize(tvsize);
        contentTv.setText(content);

    }

    //点击选择回调监听
    public interface BottomSelectListener {
        void onSelect(int id);
    }

}
